/**
 * @file tiny_initer.c
 * @brief 自动初始化器, 用于管理导出段中所有环境初始化
 * @author 张恒宣
 * @LastEditTime 2024/9/20
 *
 * MIT License
 *
 * Copyright (c) 2025 张恒宣 luckkeymail@163.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/*********************
 *      头文件
 *********************/

#include "tiny_initer.h"
#include "tiny_def.h"
#include "tiny_thread.h"

#if IS_OPEN_INITER == PROFILE_OPEN

/*********************
*    外部引用变量
*********************/

extern tiny_int32_t $Super$$main(void);

/**********************
 *      函数声明
 **********************/

USED tiny_int32_t $Sub$$main(void);

/*********************
*     私有函数 
*********************/

static void _driver_start(void) {}
static void _driver_end(void) {}
static void _moudle_start(void) {}
static void _moudle_end(void) {}
static void _application_start(void) {}
static void _application_end(void) {}	

/// @brief 初始化器相关配置
EXPORT_INIT(_driver_start, "0.end");
EXPORT_INIT(_driver_end, "1.end");
EXPORT_INIT(_moudle_start, "1.end");
EXPORT_INIT(_moudle_end, "2.end");
EXPORT_INIT(_application_start, "2.end");
EXPORT_INIT(_application_end, "3.end");

/*********************
*     私有函数 
*********************/

static initer_f_t *get_driver_start(void)
{
	return &_driver_start_init;
}

static initer_f_t *get_driver_end(void)
{
	return &_driver_end_init;
}

static initer_f_t *get_moudle_start(void)
{
	return &_moudle_start_init;
}

static initer_f_t *get_moudle_end(void)
{
	return &_moudle_end_init;
}

static initer_f_t *get_application_start(void)
{
	return &_application_start_init;
}

static initer_f_t *get_application_end(void)
{
	return &_application_end_init;
}

static void init_driver(void)
{
	for (initer_f_t *i = get_driver_start(); i <= get_driver_end(); i++)
	{
		(*i)();
	}
}

static void init_moudle(void)
{
	for (initer_f_t *i = get_moudle_start(); i <= get_moudle_end(); i++)
	{
		(*i)();
	}
}

static void init_application(void)
{
	for (initer_f_t *i = get_application_start(); i <= get_application_end(); i++)
	{
		(*i)();
	}
}

/*********************
*     公共函数 
*********************/

/// @brief 运行在main函数之前, 初始化所有配置, 启动内核.
/// @param  无需
tiny_int32_t $Sub$$main(void)
{
	init_driver();
	$Super$$main();
	init_moudle();
	init_application();
	tiny_kernel_startup();
	return 0;
}

#endif
